package com.terrynoya.fight.data.parser.sff
{
	import com.terrynoya.fight.data.parser.sff.v1.SFFReaderBase;
	
	import flash.utils.ByteArray;
	import flash.utils.Endian;

	/**
	 * sffv2
	 * @author yaojianzhou
	 */
	public class SFFReader extends SFFReaderBase
	{
		private var _spriteNodes:Array;
		private var _palNodes:Array;

		public static const ONE_PALLET_LENGTH:int = 16;				//单个调色板信息头长度
		public static const ONE_SPRITE_HEAD_LENGTH:int = 28;		//单个Sprite信息头长度
		
		/**
		 * sff file byteArray
		 * @param bytes
		 */
		public function SFFReader(bytes:ByteArray)
		{
			super(bytes);
		}
		
		/**
		 * v2版本解析 
		 * @param bytes
		 * 
		 */		
		override protected function parseSFF(bytes:ByteArray):void
		{
			this.parsePalHead(bytes);
			this.parseSpriteHead(bytes);
			
			var ldata:ByteArray = this.getData(this.header.ldataOffset,this.header.ldataLen,bytes);
			var tdata:ByteArray = this.getData(this.header.tdataOffset,this.header.tdataLen,bytes);
			
			this.parseSpriteData(ldata, tdata);
			this.parsePalData(ldata);
		}
		
		/**
		 * 得到元数据中的某段数据段 
		 * @param offset
		 * @param dataLen
		 * @param source
		 * @return 
		 * 
		 */		
		private function getData(offset:int,dataLen:int,source:ByteArray):ByteArray
		{
			var rlt:ByteArray = new ByteArray();
			source.position = offset;
			if (this.header.ldataLen > 0)
			{
				source.readBytes(rlt, 0, dataLen);
			}	
			return rlt;
		}

		/**
		 * 解析Sprite信息，图像的像素信息 
		 * @param ldata
		 * @param tdata
		 * 
		 */		
		private function parseSpriteData(ldata:ByteArray, tdata:ByteArray):void
		{
			for (var i:int=0; i < this._spriteNodes.length; i++)
			{
				var spriteNode:SFFSpriteNode=this._spriteNodes[i];
				var source:ByteArray;
				switch(spriteNode.flag)
				{
					case SFFDataType.L_DATA:
					{
						source = ldata;
						break;
					}
					case SFFDataType.T_DATA:
					{
						source = tdata;
						break;
					}
				}
				if(source == null)
				{
					continue;
				}
				var data:ByteArray = this.getData(spriteNode.offset,spriteNode.len,source);
				data.endian = Endian.LITTLE_ENDIAN;
				spriteNode.data = data;
			}
		}
		
		/**
		 * 根据调色板头解析调色板颜色信息
		 * @param bytes
		 * 
		 */		
		private function parsePalData(bytes:ByteArray):void
		{
			for (var i:int=0; i < this._palNodes.length; i++)
			{
				var palNode:SFFPalNode=this._palNodes[i];
				var palDataBytes:Bytes=new Bytes(palNode.length);
				palNode.data = this.getData(palNode.offset,palNode.length,bytes);
			}
		}

		/**
		 * 解析Sprite头信息 
		 * @param bytes
		 * @return 
		 * 
		 */		
		private function parseSpriteHead(bytes:ByteArray):void
		{
			bytes.position = this.header.offsetFirstNode;
			this._spriteNodes=new Array();
			var spriteCount:int=this.header.totalNumSprites;
			for (var i:int=0; i < spriteCount; i++)
			{
				var spHeadBytes:ByteArray=new ByteArray();
				bytes.readBytes(spHeadBytes, 0, ONE_SPRITE_HEAD_LENGTH);
				var spriteNode:SFFSpriteNode = new SFFSpriteNode(spHeadBytes);
				this._spriteNodes.push(spriteNode);
//SFFDebug.printSpriteNode(spriteNode);
			}
		}

		/**
		 * 解析调色板头信息 
		 * @param bytes
		 * @return 
		 * 
		 */		
		private function parsePalHead(bytes:ByteArray):void
		{
			bytes.position = this.header.offsetFirstPalette;
			this._palNodes = new Array();
			var palCount:int=this.header.totalNumPallets;
			for (var i:int=0; i < palCount; i++)
			{
				var palHeadBytes:ByteArray=new ByteArray();
				bytes.readBytes(palHeadBytes, 0, ONE_PALLET_LENGTH);
				var palNode:SFFPalNode = new SFFPalNode(palHeadBytes);
				this._palNodes.push(palNode);
			}
		}

		/**
		 *
		 * @param index
		 * @return
		 */
		public function getPalNode(index:int):SFFPalNode
		{
			return this._palNodes[index];
		}

		/**
		 *
		 * @return
		 */
		public function get spriteNodes():Array
		{
			return _spriteNodes;
		}
	}
}